package ${basePackage}.service.impl;

import ${common}.constant.CommonEnum;
import ${common}.core.AbstractService;
import ${common}.core.PageResult;
import ${common}.util.IdGen;
import ${common}.util.ListUtil;
import ${common}.constant.CommonConstant;
import ${mapperReference};
import ${modelReference};
import ${basePackage}.service.${tableInfo.className}Service;
import ${basePackage}.vo.entity.${tableInfo.className}EntityVO;
import ${basePackage}.vo.request.${tableInfo.className}RequestVO;
import ${basePackage}.vo.response.${tableInfo.className}ResponseVO;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * ${tableInfo.tableComment}
 *
 * @author ${author}
 * @date ${date}
 **/
@Slf4j
@Service
@Transactional
public class ${tableInfo.className}ServiceImpl extends AbstractService<${tableInfo.className}> implements ${tableInfo.className}Service {

    @Resource
    private ${tableInfo.className}Mapper ${tableInfo.varName}Mapper;

<#if varNameList?seq_contains("id")>
    /**
     * 获取${tableInfo.tableComment}分页列表
     *
     * @param ${tableInfo.varName}RequestVO ${tableInfo.tableComment}查询条件
     * @return pageResult ${tableInfo.tableComment}分页结果
     */
    @Override
    public PageResult<${tableInfo.className}ResponseVO> query${tableInfo.className}PageList(${tableInfo.className}RequestVO ${tableInfo.varName}RequestVO) {
        //
        if (${tableInfo.varName}RequestVO.getPageNumber() == null) ${tableInfo.varName}RequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (${tableInfo.varName}RequestVO.getPageSize() == null) ${tableInfo.varName}RequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<${tableInfo.className}ResponseVO> ${tableInfo.varName}ResponseVOList = Collections.emptyList();
        Condition condition = new Condition(${tableInfo.className}.class);
        Example.Criteria criteria = condition.createCriteria();
        <#--if (!StringUtils.isEmpty(${tableInfo.varName}RequestVO.getLoginName())){
            criteria.andLike("loginName", "%" + ${tableInfo.varName}RequestVO.getLoginName() + "%");
        }
        if (!StringUtils.isEmpty(${tableInfo.varName}RequestVO.getLoginName())){
            criteria.andEqualTo("loginName", ${tableInfo.varName}RequestVO.getLoginName());
        }-->
        //
        //if (StringUtils.isNotBlank(${tableInfo.varName}RequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + ${tableInfo.varName}RequestVO.getDesc() + "%");
        //}
        <#if varNameList?seq_contains("deleteFlag")>
        //删除状态
        criteria.andEqualTo("deleteFlag", CommonEnum.StateFlagEnum.STATE_FLAG_NORMAL.getCode());
        </#if>
        <#if varNameList?seq_contains("createDate")>
        //排序
        condition.orderBy("createDate").desc();
        </#if>
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(${tableInfo.varName}RequestVO.getPageNumber(), ${tableInfo.varName}RequestVO.getPageSize());
            List<${tableInfo.className}> ${tableInfo.varName}List = this.selectByCondition(condition);
            ${tableInfo.varName}ResponseVOList = ListUtil.convertList(${tableInfo.varName}List, ${tableInfo.className}ResponseVO.class);
        }
        //
        return PageResult.of(total, ${tableInfo.varName}ResponseVOList);
    }
</#if>

    /**
     * 获取${tableInfo.tableComment}详细信息
     *
     * @param ${varName(tableInfo.relationColumn)} ${tableInfo.tableComment}${tableInfo.relationColumn}
     * @return ${tableInfo.tableComment}
     */
    @Override
    public List<${tableInfo.className}EntityVO> get${tableInfo.className}ListBy${className(tableInfo.relationColumn)}(String ${varName(tableInfo.relationColumn)}) {
        Condition condition = new Condition(${tableInfo.className}.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("${varName(tableInfo.relationColumn)}", ${varName(tableInfo.relationColumn)});
        List<${tableInfo.className}> list = this.selectByCondition(condition);
        List<${tableInfo.className}EntityVO> voList = ListUtil.convertList(list, ${tableInfo.className}EntityVO.class);
        return voList;
    }

    /**
     * 替换${tableInfo.tableComment}
     *
     * @param list ${tableInfo.tableComment}实体列表
     * @param ${varName(tableInfo.relationColumn)} ${tableInfo.tableComment}${tableInfo.relationColumn}
     */
    @Override
    public void replace${tableInfo.className}(String ${varName(tableInfo.relationColumn)}, List<${tableInfo.className}EntityVO> list) {
        Condition condition = new Condition(${tableInfo.className}.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("${varName(tableInfo.relationColumn)}", ${varName(tableInfo.relationColumn)});
        this.deleteByCondition(condition);
        //
        if (ListUtil.isNotEmpty(list)) {
            List<${tableInfo.className}> convertList = ListUtil.convertList(list, ${tableInfo.className}.class);
            this.${tableInfo.varName}Mapper.batchInsert${tableInfo.className}(convertList);
        }
    }

<#if varNameList?seq_contains("id")>
    /**
     * 删除${tableInfo.tableComment}
     *
     * @param id ${tableInfo.tableComment}ID
     */
    @Override
    public void delete${tableInfo.className}(String id) {
        <#if varNameList?seq_contains("deleteFlag")>
        ${tableInfo.className} ${tableInfo.varName} = new ${tableInfo.className}();
        ${tableInfo.varName}.setId(id);
        ${tableInfo.varName}.setDeleteFlag(CommonEnum.StateFlagEnum.STATE_FLAG_DELETE.getCode());
        //逻辑删除
        this.updateByPrimaryKeySelective(${tableInfo.varName});
        <#else>
        this.deleteByPrimaryKey(id);
        </#if>
    }
</#if>
}
